package com.demo.core.dao.impl;

import java.util.List;
import java.util.Map;

import com.demo.core.cache.CacheKey;
import com.demo.core.cache.CacheWrapper;
import com.demo.core.dao.RoleFunctionDao;
import com.demo.core.dao.impl.base.BaseCoreDaoSupport;
import com.demo.core.model.RoleFunction;
import com.demo.core.model.support.GeneralExecuteModel;

/**
 * 角色功能数据访问层实现类
 * 
 * @author ryuu.kk
  */
public class RoleFunctionDaoImpl extends BaseCoreDaoSupport<RoleFunction, Long> implements RoleFunctionDao {

	/**
	 * 站点空间 
	 */
	protected final static String ROLE_FUNCTION_SPACE = "roleFunctionSpace.";
	/**
	 * 模型表名
	 */
	protected final static String TABLE_NAME = "T_CORE_ROLE_FUNCTION";
	/**
	 * 模型表主键ID
	 */
	protected final static String TABLE_PK_NAME = "ID";

	@Override
	protected String nameSpace() {
		return ROLE_FUNCTION_SPACE;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<RoleFunction> queryFunctionUnion(Map<String, Object> params) {
		CacheKey key = null;
		List<RoleFunction> result = null;
		CacheWrapper<CacheKey, Object> cache = null;
		
		if (isUseCache() && (cache = getCache()) != null) {
			key = new CacheKey();
			// 增加一个条件,防止key的HashCode无法区分普通检索和联合查询.
			key.update("queryRoleFunction");
			key.update(params.hashCode());
			result = (List<RoleFunction>) cache.get(key);
		}
		
		if (result == null) {
			result  = getSqlMapClientTemplate().queryForList(ROLE_FUNCTION_SPACE + "queryRoleFunction", params);
			if (isUseCache() && cache != null && result != null) {
				cache.put(key, result);
			}
		}
		return result;
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public Map<Long, RoleFunction> queryFunctionMap(Map<String, Object> params) {
		CacheKey key = null;
		Map<Long, RoleFunction> result = null;
		CacheWrapper<CacheKey, Object> cache = null;
		
		if (isUseCache() && (cache = getCache()) != null) {
			key = new CacheKey();
			// 增加一个条件,防止key的HashCode无法区分普通检索和联合查询.
			key.update("queryRoleFunctionMap");
			key.update(params.hashCode());
			result = (Map<Long, RoleFunction>) cache.get(key);
		}
		
		if (result == null) {
			result  = getSqlMapClientTemplate().queryForMap(ROLE_FUNCTION_SPACE + "queryRoleFunctionMap", params, "siteFunctionId");
			if (isUseCache() && cache != null && result != null) {
				cache.put(key, result);
			}
		}
		return result;
	}
	
	@Override
	protected void doInit(GeneralExecuteModel<Long> model) {
		model.setPk(TABLE_PK_NAME);
		model.setTable(TABLE_NAME);
	}
}